package cn.fl.project.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.DateUtils;
import cn.cmm.base.framework.utils.DecimalUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.customer.vo.CstBscInfoVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.project.condition.PrjCstAssMbInfoQueryCondition;
import cn.fl.project.constant.PrjCstAssMbInfoConstant;
import cn.fl.project.service.IPrjBscInfoService;
import cn.fl.project.service.IPrjCstAssMbInfoService;
import cn.fl.project.vo.PrjCstAssMbInfoVO;
import cn.fl.system.constant.SysLogConstant;
import net.sf.json.JSONArray;

/**
 * @Description: 项目客户资产抵押担保信息表 控制层
 */
@Controller
@RequestMapping("/project/prjcstassmbInfo")
public class PrjCstAssMbInfoController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(PrjCstAssMbInfoController.class);

	/**
	 * PrjCstAssMbInfo 服务层BEAN
	 */
	@Autowired
	private IPrjCstAssMbInfoService prjCstAssMbInfoService;
	

	/**
	 * prjBscInfo 服务层BEAN
	 */
	@Autowired
	private IPrjBscInfoService prjBscInfoService;
	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		String prjId = request.getParameter("prjId");//项目ID
		CstBscInfoVO cstBscInfoVO = null; 
		String detailFlag = request.getParameter("detailFlag");
		String cstRlCd = request.getParameter("cstRlCd");//客户关系
				
		String cstType = "";
		Long cstId=null;
		if(StringUtils.isNotBlank(prjId)){
			cstBscInfoVO = prjBscInfoService.getCstTypByPrjId(Long.valueOf(prjId));
			if(null!=cstBscInfoVO){
				 cstType = cstBscInfoVO.getCstTypCd();
				 cstId= cstBscInfoVO.getId();
			}
		}
		ModelAndView view = new ModelAndView("project/prjCstAssMbInfo/prjCstAssMbInfoList");
		BaseLogUtils.info(logger, "toListPage", "跳转到列表页面.", CurrentThreadContext.getCurrentUserName());
		view.addObject("prjId", prjId);
		view.addObject("cstType",cstType);
		view.addObject("cstId", cstId);
		view.addObject("detailFlag", detailFlag);
		view.addObject("cstRlCd", cstRlCd);
		return view;
	}
	
	/**
	 * 到设备添加页面
	 */
	@RequestMapping(value = "/toEquiAddPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toEquiAddPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("project/prjCstAssMbInfo/prjCstAssMbInfoEquiAdd");
		String cstType = request.getParameter("cstType");
		String prjId = request.getParameter("prjId");
		String cstId = request.getParameter("cstId");
		String cstRlCd = request.getParameter("cstRlCd");//客户关系
		List<KeyValueVO> assRlCdList = this.prjCstAssMbInfoService.getAssRlCdList();//与申请人关系
		List<KeyValueVO> mStsCdList = this.prjCstAssMbInfoService.getMStsCdList();//抵押状态
		view.addObject("prjId", prjId);
		view.addObject("cstType", cstType);
		view.addObject("cstId", cstId);
		view.addObject("assRlCdList", assRlCdList);
		view.addObject("mStsCdList", mStsCdList);
		view.addObject("cstRlCd", cstRlCd);
		BaseLogUtils.info(logger, "toEquiAddPage", "跳转到设备添加页面.", CurrentThreadContext.getCurrentUserName());
		return view;
	}
	/**
	 * 到房产添加页面
	 */
	@RequestMapping(value = "/toBuilAddPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toBuilAddPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("project/prjCstAssMbInfo/prjCstAssMbInfoBuilAdd");
		String cstType = request.getParameter("cstType");
		String prjId = request.getParameter("prjId");
		String cstId = request.getParameter("cstId");
		String cstRlCd = request.getParameter("cstRlCd");
		List<KeyValueVO> assRlCdList = this.prjCstAssMbInfoService.getAssRlCdList();//与申请人关系
		List<KeyValueVO> mStsCdList = this.prjCstAssMbInfoService.getMStsCdList();//抵押状态
		List<KeyValueVO> natureCdList = this.prjCstAssMbInfoService.getNatureCdList();//抵押状态
		view.addObject("prjId", prjId);
		view.addObject("cstType", cstType);
		view.addObject("cstId", cstId);
		view.addObject("assRlCdList", assRlCdList);
		view.addObject("mStsCdList", mStsCdList);
		view.addObject("natureCdList", natureCdList);
		view.addObject("cstRlCd", cstRlCd);
		BaseLogUtils.info(logger, "toModifyPage", "跳转到修改页面.", CurrentThreadContext.getCurrentUserName());
		return view;
	}
	
	/**
	 * 到其他添加页面
	 */
	@RequestMapping(value = "/toOthAddPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toOthAddPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("project/prjCstAssMbInfo/prjCstAssMbInfoOthAdd");
		String cstType = request.getParameter("cstType");
		String prjId = request.getParameter("prjId");
		String cstId = request.getParameter("cstId");
		String cstRlCd = request.getParameter("cstRlCd");//客户关系
		List<KeyValueVO> assRlCdList = this.prjCstAssMbInfoService.getAssRlCdList();//与申请人关系
		view.addObject("prjId", prjId);
		view.addObject("cstType", cstType);
		view.addObject("cstId", cstId);
		view.addObject("assRlCdList", assRlCdList);
		view.addObject("cstRlCd", cstRlCd);
		BaseLogUtils.info(logger, "toModifyPage", "跳转到修改页面.", CurrentThreadContext.getCurrentUserName());
		return view;
	}
	/**
	 * 到设备修改页面
	 */
	@RequestMapping(value = "/toEquiModifyPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toEquiModifyPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		String cstRlCd = request.getParameter("cstRlCd");
		PrjCstAssMbInfoVO prjCstAssMbInfoVO = null;
		if(StringUtils.isNotBlank(id)){
			prjCstAssMbInfoVO = this.prjCstAssMbInfoService.selectById(Long.valueOf(id));
		}
		List<KeyValueVO> assRlCdList = this.prjCstAssMbInfoService.getAssRlCdList();//与申请人关系
		List<KeyValueVO> mStsCdList = this.prjCstAssMbInfoService.getMStsCdList();//抵押状态
		ModelAndView view = new ModelAndView("project/prjCstAssMbInfo/prjCstAssMbInfoEquiAdd");
		String cstType = request.getParameter("cstType");
		view.addObject("prjId", prjCstAssMbInfoVO.getPrjId());
		view.addObject("cstType", cstType);
		view.addObject("cstId", prjCstAssMbInfoVO.getCstId());
		view.addObject("prjCstAssMbInfo", prjCstAssMbInfoVO);
		view.addObject("assRlCdList", assRlCdList);
		view.addObject("mStsCdList", mStsCdList);
		view.addObject("cstRlCd", cstRlCd);
		BaseLogUtils.info(logger, "toModifyPage", "跳转到修改页面.", CurrentThreadContext.getCurrentUserName());
		return view;
	}
	/**
	 * 到房产修改页面
	 */
	@RequestMapping(value = "/toBuilModifyPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toBuilModifyPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("project/prjCstAssMbInfo/prjCstAssMbInfoBuilAdd");
		String cstType = request.getParameter("cstType");
		String id = request.getParameter("id");
		String cstRlCd = request.getParameter("cstRlCd");//客户关系
		PrjCstAssMbInfoVO prjCstAssMbInfoVO = null;
		if(StringUtils.isNotBlank(id)){
			prjCstAssMbInfoVO = this.prjCstAssMbInfoService.selectById(Long.valueOf(id));
		}
		List<KeyValueVO> assRlCdList = this.prjCstAssMbInfoService.getAssRlCdList();//与申请人关系
		List<KeyValueVO> mStsCdList = this.prjCstAssMbInfoService.getMStsCdList();//抵押状态
		List<KeyValueVO> natureCdList = this.prjCstAssMbInfoService.getNatureCdList();//抵押状态
		view.addObject("cstType", cstType);
		view.addObject("assRlCdList", assRlCdList);
		view.addObject("mStsCdList", mStsCdList);
		view.addObject("natureCdList", natureCdList);
		view.addObject("prjId", prjCstAssMbInfoVO.getPrjId());
		view.addObject("cstType", cstType);
		view.addObject("cstId", prjCstAssMbInfoVO.getCstId());
		view.addObject("prjCstAssMbInfo", prjCstAssMbInfoVO);
		view.addObject("cstRlCd", cstRlCd);
		BaseLogUtils.info(logger, "toModifyPage", "跳转到修改页面.", CurrentThreadContext.getCurrentUserName());
		return view;
	}
	
	/**
	 * 到其他修改页面
	 */
	@RequestMapping(value = "/toOthModifyPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toOthModifyPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("project/prjCstAssMbInfo/prjCstAssMbInfoOthAdd");
		String cstType = request.getParameter("cstType");
		String id = request.getParameter("id");
		String cstRlCd = request.getParameter("cstRlCd");//客户关系
		PrjCstAssMbInfoVO prjCstAssMbInfoVO = null;
		if(StringUtils.isNotBlank(id)){
			prjCstAssMbInfoVO = this.prjCstAssMbInfoService.selectById(Long.valueOf(id));
		}
		List<KeyValueVO> assRlCdList = this.prjCstAssMbInfoService.getAssRlCdList();//与申请人关系
		view.addObject("prjId", prjCstAssMbInfoVO.getPrjId());
		view.addObject("cstType", cstType);
		view.addObject("assRlCdList", assRlCdList);
		view.addObject("cstId", prjCstAssMbInfoVO.getCstId());
		view.addObject("prjCstAssMbInfo", prjCstAssMbInfoVO);
		view.addObject("cstRlCd", cstRlCd);
		BaseLogUtils.info(logger, "toModifyPage", "跳转到修改页面.", CurrentThreadContext.getCurrentUserName());
		return view;
	}
	
	
	/**
	 * 到设备修改页面
	 */
	@RequestMapping(value = "/toEquiDetailPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toEquiDetailPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		PrjCstAssMbInfoVO prjCstAssMbInfoVO = null;
		if(StringUtils.isNotBlank(id)){
			prjCstAssMbInfoVO = this.prjCstAssMbInfoService.selectById(Long.valueOf(id));
		}
		ModelAndView view = new ModelAndView("project/prjCstAssMbInfo/prjCstAssMbInfoEquiDetail");
		view.addObject("prjCstAssMbInfo", prjCstAssMbInfoVO);
		BaseLogUtils.info(logger, "toModifyPage", "跳转到修改页面.", CurrentThreadContext.getCurrentUserName());
		return view;
	}
	/**
	 * 到房产修改页面
	 */
	@RequestMapping(value = "/toBuilDetailPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toBuilDetailPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("project/prjCstAssMbInfo/prjCstAssMbInfoBuilDetail");
		String id = request.getParameter("id");
		PrjCstAssMbInfoVO prjCstAssMbInfoVO = null;
		if(StringUtils.isNotBlank(id)){
			prjCstAssMbInfoVO = this.prjCstAssMbInfoService.selectById(Long.valueOf(id));
		}
		view.addObject("prjCstAssMbInfo", prjCstAssMbInfoVO);
		BaseLogUtils.info(logger, "toModifyPage", "跳转到修改页面.", CurrentThreadContext.getCurrentUserName());
		return view;
	}
	
	/**
	 * 到其他修改页面
	 */
	@RequestMapping(value = "/toOthDetailPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toOthDetailPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("project/prjCstAssMbInfo/prjCstAssMbInfoOthDetail");
		String id = request.getParameter("id");
		PrjCstAssMbInfoVO prjCstAssMbInfoVO = null;
		if(StringUtils.isNotBlank(id)){
			prjCstAssMbInfoVO = this.prjCstAssMbInfoService.selectById(Long.valueOf(id));
		}
		view.addObject("prjCstAssMbInfo", prjCstAssMbInfoVO);
		BaseLogUtils.info(logger, "toModifyPage", "跳转到修改页面.", CurrentThreadContext.getCurrentUserName());
		return view;
	}
	/**
	 * 到修改页面
	 */
	@RequestMapping(value = "/toModifyPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("project/prjCstAssMbInfo/modify");
		String cstType = request.getParameter("cstType");
		view.addObject("cstType", cstType);
		BaseLogUtils.info(logger, "toModifyPage", "跳转到修改页面.", CurrentThreadContext.getCurrentUserName());
		return view;
	}

	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String searchName = request.getParameter("demo.name");
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(currentPage)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		PrjCstAssMbInfoQueryCondition qc = new PrjCstAssMbInfoQueryCondition();
		if (StringUtils.isNotEmpty(searchName)) {
			//TODO SOMTHING
		}
		page.setCondition(qc);

		//TODO SOMETHING
		Pager pager = new Pager();//this.prjCstAssMbInfoService.searchListPage(page);
		List<PrjCstAssMbInfoVO> list = (List<PrjCstAssMbInfoVO>) pager.getResultList();
		ListPageVO<PrjCstAssMbInfoVO> listPage = new ListPageVO<PrjCstAssMbInfoVO>(list, pager.getTotalCount());
		BaseLogUtils.info(logger, "initList", "查询数据完成. DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
		return listPage;
	}
	
	/**
	 * 获取抵押物
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/initPrjCstAssMbInfoList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initPrjCstAssMbInfoList(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		
		String assPropCd = request.getParameter("assPropCd"); //资产属性
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String prjId = request.getParameter("prjId");
		String cstId = request.getParameter("cstId");
		String cstRlCd = request.getParameter("cstRlCd");//客户关系
		Pager pager = null;
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		try {
			Pager page = new Pager();
			if (StringUtils.isNotEmpty(currentPage)) {
				page.setCurrentPage(Integer.parseInt(currentPage));
			}

			

			PrjCstAssMbInfoQueryCondition qc = new PrjCstAssMbInfoQueryCondition();
			if(StringUtils.isNotBlank(assPropCd)){
				qc.setAssPropCd(assPropCd);
			}
			if (StringUtils.isNotEmpty(prjId)) {
				qc.setPrjId(Long.valueOf(prjId));
			}
			if (StringUtils.isNotEmpty(cstId)) {
				qc.setCstId(Long.valueOf(cstId));
			}
			if(StringUtils.isNotBlank(cstRlCd)){
				qc.setCstRlCd(cstRlCd);
			}
			page.setCondition(qc);
			pager = this.prjCstAssMbInfoService.searchListPage(page);
			BaseLogUtils.info(logger, "initPrjCstAssMbInfoList", "获取数据.", CurrentThreadContext.getCurrentUserName());
		}catch(BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "initPrjCstAssMbInfoList", "获取失败."+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch(Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "getById", "获取失败"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		List<PrjCstAssMbInfoVO> list = (List<PrjCstAssMbInfoVO>) pager.getResultList();
		ListPageVO<PrjCstAssMbInfoVO> listPage = new ListPageVO<PrjCstAssMbInfoVO>(list, pager.getTotalCount());
		return listPage;
	}


	/**
	 * 删除数据
	 */
	@RequestMapping(value = "/removeByIds.do", method = {RequestMethod.POST })
	@ResponseBody
	public DataResultVO removeByIds(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String ids = request.getParameter("ids");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);
		if(StringUtils.isEmpty(ids)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			return result;
		}
		List<Long> idList = new ArrayList<Long>();
		JSONArray idsArray =JSONArray.fromObject(ids);
		for (Object object : idsArray) {
			idList.add(Long.valueOf(object.toString()));
		}
		try {
			this.prjCstAssMbInfoService.removeByIds(idList);
			BaseLogUtils.info(logger, "removeById", "删除数据.[ids="+ids+"]", CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "removeById", "删除数据失败.[ids="+ids+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "removeById", "删除数据失败.[id="+ids+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}

		return result;
	}
	

	/**
	 * 得到一条记录
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getById.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.error(logger, "getById", "获取数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		
		try {
			PrjCstAssMbInfoVO vo = this.prjCstAssMbInfoService.selectById(Long.valueOf(id));
			if(null == vo){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_QUERY_DATA_IS_EMPTY);
				BaseLogUtils.info(logger, "getById", "获取数据,数据不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			}
			result.setData(vo);
			BaseLogUtils.info(logger, "getById", "获取数据.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		}catch(BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getById", "获取失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch(Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "getById", "获取失败.[id="+id+"]"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

	/**
	 * 新增
	 */
	@RequestMapping(value = "/add.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object add(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
		PrjCstAssMbInfoVO entity = null;
		String id = request.getParameter("id");
		String type = request.getParameter("assPropCd");
		Long prjCstId = null;
		try {
			entity =getPrjCstAssMbInfoParam(request,type); //获取参数
			if(StringUtils.isNotBlank(id)){
				//修改客户抵押信息
				entity.setId(Long.valueOf(id));
				this.prjCstAssMbInfoService.modify(entity);
			}else{
				//添加客户抵押信息
				entity.setCrtTm(new Date());
				entity.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
				entity.setIsDel(BaseConstant.IS_YESNO_NO);
				prjCstId = this.prjCstAssMbInfoService.add(entity);
			}
			result.setData(prjCstId);
			BaseLogUtils.info(logger, "add", "新增数据.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "add", "新增数据失败.DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "add", "新增数据失败.DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}

		return result;
	}

	private PrjCstAssMbInfoVO getPrjCstAssMbInfoParam(HttpServletRequest request,String type) {
		PrjCstAssMbInfoVO prjCstAssMbInfo = new  PrjCstAssMbInfoVO();
		String prjId = request.getParameter("prjId");//项目id
		String cstId = request.getParameter("cstId");//客户ID
		String assPropCd = request.getParameter("assPropCd");//资产属性
		String assNm = request.getParameter("assNm");//资产名称
		String brNm = request.getParameter("brNm");//资产品牌
		String assOwner = request.getParameter("assOwner");// 所有权人
		String assRlCd = request.getParameter("assRlCd");//与申请人关系
		String mStsCd = request.getParameter("mStsCd");//抵押状况
		String purchDt = request.getParameter("purchDt");//购买时间
		String untPc = request.getParameter("untPc");//买入价格（万元）
		String value = request.getParameter("value");//现估值（万元）
		String assNo = request.getParameter("assNo");//设备号牌/房产登记号
		String assSts = request.getParameter("assSts");//ASS_STS
		String natureCd = request.getParameter("natureCd");//权属性质
		String area = request.getParameter("area");//面积（平方米）
		String addr = request.getParameter("addr");//地址
		String cstRlCd = request.getParameter("cstRlCd");//客户关系
		String assRem = request.getParameter("assRem");
		String othAssTyp = request.getParameter("othAssTyp");//其他资产类型
		prjCstAssMbInfo.setPrjId(Long.valueOf(prjId));
		prjCstAssMbInfo.setCstId(Long.valueOf(cstId));
		prjCstAssMbInfo.setCstRlCd(cstRlCd);
		prjCstAssMbInfo.setAssRem(assRem);
		prjCstAssMbInfo.setOthAssTyp(othAssTyp);
		if(type.equals(PrjCstAssMbInfoConstant.CST_ASS_PROP_EQUI_CD)){
			if(StringUtils.isBlank(assNm)){
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
			}
			if(StringUtils.isBlank(untPc)){
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"买入价格不能为空！");
			}
			if(StringUtils.isBlank(assOwner)){
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"所有权人不能为空！");
			}
			if(StringUtils.isBlank(assNo)){
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"号牌不能为空！");
			}
			if(StringUtils.isBlank(assSts)){
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"设备状况不能为空！");
			}
		}
		if(type.equals(PrjCstAssMbInfoConstant.CST_ASS_PROP_BUIL_CD)){
			if(StringUtils.isBlank(assOwner)){
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"所有权人不能为空！");
			}
			if(StringUtils.isBlank(area)){
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"使用面积不能为空！");
			}
			if(StringUtils.isBlank(value)){
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"估值不能为空！");
			}
			if(StringUtils.isBlank(addr)){
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"地址不能为空！");
			}
			if(StringUtils.isBlank(assNo)){
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"房产登记号不能为空！");
			}
		}
		if(type.equals(PrjCstAssMbInfoConstant.CST_ASS_PROP_OTH_CD)){
			if(StringUtils.isBlank(assNm)){
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
			}
			if(StringUtils.isBlank(assOwner)){
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"所有权人不能为空！");
			}
			if(StringUtils.isBlank(value)){
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"现估值不能为空！");
			}
			if(StringUtils.isBlank(assSts)){
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"财产状况不能为空！");
			}
		}
			prjCstAssMbInfo.setAssPropCd(assPropCd);
	
			prjCstAssMbInfo.setAssNm(assNm);
			prjCstAssMbInfo.setBrNm(brNm);
	
			prjCstAssMbInfo.setAssOwner(assOwner);
			prjCstAssMbInfo.setAssRlCd(assRlCd);
			prjCstAssMbInfo.setMStsCd(mStsCd);
		if(StringUtils.isNotBlank(purchDt)){
			prjCstAssMbInfo.setPurchDt(DateUtils.formatStringToDate(purchDt, DateUtils.YYYY_MM_DD));
		}
		
		if(StringUtils.isNotBlank(untPc)){
			if(!StringUtils.isUnsignedNumeric(untPc)){
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"买入价格应为数字！");
			}
			prjCstAssMbInfo.setUntPc(DecimalUtils.convertYuanToFen(Double.valueOf(untPc)).doubleValue());
		}
		
		if(StringUtils.isNotBlank(value)){
			if(!StringUtils.isUnsignedNumeric(value)){
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"估值应为数字！");
			}
			prjCstAssMbInfo.setValue(DecimalUtils.convertYuanToFen(Double.valueOf(value)).doubleValue());
		}
			prjCstAssMbInfo.setAssNo(assNo);
			prjCstAssMbInfo.setAssSts(assSts);
			prjCstAssMbInfo.setNatureCd(natureCd);
			if(StringUtils.isNotBlank(area)){
				if(!StringUtils.isUnsignedNumeric(area)){
					throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"面积应为数字！");
				}
				prjCstAssMbInfo.setArea(Double.valueOf(area));	
			}
		
			prjCstAssMbInfo.setAddr(addr);
		prjCstAssMbInfo.setMdfTm(new Date());
		prjCstAssMbInfo.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
		return prjCstAssMbInfo;
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/modify.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modify(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "modify", "修改数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		PrjCstAssMbInfoVO entity = null;
		try {
			entity = new PrjCstAssMbInfoVO();
			entity.setId(Long.valueOf(id));
			//entity.setName(name);
			//TODO ADD PARAM
			boolean success = this.prjCstAssMbInfoService.modify(entity);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
				BaseLogUtils.info(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
			}
			BaseLogUtils.info(logger, "modify", "修改成功.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity) + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
	
	/**
	 * 到担保人家庭财产情况列表页面
	 */
	@RequestMapping(value = "/toGuarListPage.do", method = { RequestMethod.GET})
	public ModelAndView toGuarListPage(HttpServletRequest request, HttpServletResponse response) {
		String prjId = request.getParameter("prjId");//项目ID
		String detailFlag = request.getParameter("detailFlag");
		String cstRlCd = request.getParameter("cstRlCd");//客户关系
		String cstId = request.getParameter("cstId");//客户ID
		String cstType = request.getParameter("cstTypCd");//客户类型
		ModelAndView view = new ModelAndView("project/prjCstAssMbInfo/prjGuarAssMbInfoList");
		BaseLogUtils.info(logger, "toListPage", "跳转到列表页面.", CurrentThreadContext.getCurrentUserName());
		view.addObject("prjId", prjId);
		view.addObject("cstType",cstType);
		view.addObject("cstId", cstId);
		view.addObject("detailFlag", detailFlag);
		view.addObject("cstRlCd", cstRlCd);
		return view;
	}
	/**
	 * 到担保人设备添加页面
	 */
	@RequestMapping(value = "/toGuarEquiAddPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toGuarEquiAddPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("project/prjCstAssMbInfo/prjGuarAssMbInfoEquiAdd");
		String cstType = request.getParameter("cstType");
		String prjId = request.getParameter("prjId");
		String cstId = request.getParameter("cstId");
		String cstRlCd = request.getParameter("cstRlCd");//客户关系
		List<KeyValueVO> assRlCdList = this.prjCstAssMbInfoService.getAssRlCdList();//与申请人关系
		List<KeyValueVO> mStsCdList = this.prjCstAssMbInfoService.getMStsCdList();//抵押状态
		view.addObject("prjId", prjId);
		view.addObject("cstType", cstType);
		view.addObject("cstId", cstId);
		view.addObject("assRlCdList", assRlCdList);
		view.addObject("mStsCdList", mStsCdList);
		view.addObject("cstRlCd", cstRlCd);
		BaseLogUtils.info(logger, "toEquiAddPage", "跳转到设备添加页面.", CurrentThreadContext.getCurrentUserName());
		return view;
	}
	/**
	 * 到担保人房产添加页面
	 */
	@RequestMapping(value = "/toGuarBuilAddPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toGuarBuilAddPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("project/prjCstAssMbInfo/prjGuarAssMbInfoBuilAdd");
		String cstType = request.getParameter("cstType");
		String prjId = request.getParameter("prjId");
		String cstId = request.getParameter("cstId");
		String cstRlCd = request.getParameter("cstRlCd");
		List<KeyValueVO> assRlCdList = this.prjCstAssMbInfoService.getAssRlCdList();//与申请人关系
		List<KeyValueVO> mStsCdList = this.prjCstAssMbInfoService.getMStsCdList();//抵押状态
		List<KeyValueVO> natureCdList = this.prjCstAssMbInfoService.getNatureCdList();//抵押状态
		view.addObject("prjId", prjId);
		view.addObject("cstType", cstType);
		view.addObject("cstId", cstId);
		view.addObject("assRlCdList", assRlCdList);
		view.addObject("mStsCdList", mStsCdList);
		view.addObject("natureCdList", natureCdList);
		view.addObject("cstRlCd", cstRlCd);
		BaseLogUtils.info(logger, "toModifyPage", "跳转到修改页面.", CurrentThreadContext.getCurrentUserName());
		return view;
	}
	
	/**
	 * 到担保人其他添加页面
	 */
	@RequestMapping(value = "/toGuarOthAddPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toGuarOthAddPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("project/prjCstAssMbInfo/prjGuarAssMbInfoOthAdd");
		String cstType = request.getParameter("cstType");
		String prjId = request.getParameter("prjId");
		String cstId = request.getParameter("cstId");
		String cstRlCd = request.getParameter("cstRlCd");//客户关系
		List<KeyValueVO> assRlCdList = this.prjCstAssMbInfoService.getAssRlCdList();//与申请人关系
		view.addObject("prjId", prjId);
		view.addObject("cstType", cstType);
		view.addObject("cstId", cstId);
		view.addObject("assRlCdList", assRlCdList);
		view.addObject("cstRlCd", cstRlCd);
		BaseLogUtils.info(logger, "toModifyPage", "跳转到修改页面.", CurrentThreadContext.getCurrentUserName());
		return view;
	}
	/**
	 * 到担保人设备修改页面
	 */
	@RequestMapping(value = "/toGuarEquiModifyPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toGuarEquiModifyPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		String cstRlCd = request.getParameter("cstRlCd");
		PrjCstAssMbInfoVO prjCstAssMbInfoVO = null;
		if(StringUtils.isNotBlank(id)){
			prjCstAssMbInfoVO = this.prjCstAssMbInfoService.selectById(Long.valueOf(id));
		}
		List<KeyValueVO> assRlCdList = this.prjCstAssMbInfoService.getAssRlCdList();//与申请人关系
		List<KeyValueVO> mStsCdList = this.prjCstAssMbInfoService.getMStsCdList();//抵押状态
		ModelAndView view = new ModelAndView("project/prjCstAssMbInfo/prjGuarAssMbInfoEquiAdd");
		String cstType = request.getParameter("cstType");
		view.addObject("prjId", prjCstAssMbInfoVO.getPrjId());
		view.addObject("cstType", cstType);
		view.addObject("cstId", prjCstAssMbInfoVO.getCstId());
		view.addObject("prjCstAssMbInfo", prjCstAssMbInfoVO);
		view.addObject("assRlCdList", assRlCdList);
		view.addObject("mStsCdList", mStsCdList);
		view.addObject("cstRlCd", cstRlCd);
		BaseLogUtils.info(logger, "toModifyPage", "跳转到修改页面.", CurrentThreadContext.getCurrentUserName());
		return view;
	}
	/**
	 * 到担保人房产修改页面
	 */
	@RequestMapping(value = "/toGuarBuilModifyPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toGuarBuilModifyPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("project/prjCstAssMbInfo/prjGuarAssMbInfoBuilAdd");
		String cstType = request.getParameter("cstType");
		String id = request.getParameter("id");
		String cstRlCd = request.getParameter("cstRlCd");//客户关系
		PrjCstAssMbInfoVO prjCstAssMbInfoVO = null;
		if(StringUtils.isNotBlank(id)){
			prjCstAssMbInfoVO = this.prjCstAssMbInfoService.selectById(Long.valueOf(id));
		}
		List<KeyValueVO> assRlCdList = this.prjCstAssMbInfoService.getAssRlCdList();//与申请人关系
		List<KeyValueVO> mStsCdList = this.prjCstAssMbInfoService.getMStsCdList();//抵押状态
		List<KeyValueVO> natureCdList = this.prjCstAssMbInfoService.getNatureCdList();//抵押状态
		view.addObject("cstType", cstType);
		view.addObject("assRlCdList", assRlCdList);
		view.addObject("mStsCdList", mStsCdList);
		view.addObject("natureCdList", natureCdList);
		view.addObject("prjId", prjCstAssMbInfoVO.getPrjId());
		view.addObject("cstType", cstType);
		view.addObject("cstId", prjCstAssMbInfoVO.getCstId());
		view.addObject("prjCstAssMbInfo", prjCstAssMbInfoVO);
		view.addObject("cstRlCd", cstRlCd);
		BaseLogUtils.info(logger, "toModifyPage", "跳转到修改页面.", CurrentThreadContext.getCurrentUserName());
		return view;
	}
	
	/**
	 * 到担保人其他修改页面
	 */
	@RequestMapping(value = "/toGuarOthModifyPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toGuarOthModifyPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("project/prjCstAssMbInfo/prjGuarAssMbInfoOthAdd");
		String cstType = request.getParameter("cstType");
		String id = request.getParameter("id");
		String cstRlCd = request.getParameter("cstRlCd");//客户关系
		PrjCstAssMbInfoVO prjCstAssMbInfoVO = null;
		if(StringUtils.isNotBlank(id)){
			prjCstAssMbInfoVO = this.prjCstAssMbInfoService.selectById(Long.valueOf(id));
		}
		List<KeyValueVO> assRlCdList = this.prjCstAssMbInfoService.getAssRlCdList();//与申请人关系
		view.addObject("prjId", prjCstAssMbInfoVO.getPrjId());
		view.addObject("cstType", cstType);
		view.addObject("assRlCdList", assRlCdList);
		view.addObject("cstId", prjCstAssMbInfoVO.getCstId());
		view.addObject("prjCstAssMbInfo", prjCstAssMbInfoVO);
		view.addObject("cstRlCd", cstRlCd);
		BaseLogUtils.info(logger, "toModifyPage", "跳转到修改页面.", CurrentThreadContext.getCurrentUserName());
		return view;
	}
	
	
	/**
	 * 到担保人设备详情页面
	 */
	@RequestMapping(value = "/toGuarEquiDetailPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toGuarEquiDetailPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		PrjCstAssMbInfoVO prjCstAssMbInfoVO = null;
		if(StringUtils.isNotBlank(id)){
			prjCstAssMbInfoVO = this.prjCstAssMbInfoService.selectById(Long.valueOf(id));
		}
		ModelAndView view = new ModelAndView("project/prjCstAssMbInfo/prjGuarAssMbInfoEquiDetail");
		view.addObject("prjCstAssMbInfo", prjCstAssMbInfoVO);
		BaseLogUtils.info(logger, "toModifyPage", "跳转到修改页面.", CurrentThreadContext.getCurrentUserName());
		return view;
	}
	/**
	 * 到担保人房产详情页面
	 */
	@RequestMapping(value = "/toGuarBuilDetailPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toGuarBuilDetailPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("project/prjCstAssMbInfo/prjGuarAssMbInfoBuilDetail");
		String id = request.getParameter("id");
		PrjCstAssMbInfoVO prjCstAssMbInfoVO = null;
		if(StringUtils.isNotBlank(id)){
			prjCstAssMbInfoVO = this.prjCstAssMbInfoService.selectById(Long.valueOf(id));
		}
		view.addObject("prjCstAssMbInfo", prjCstAssMbInfoVO);
		BaseLogUtils.info(logger, "toModifyPage", "跳转到修改页面.", CurrentThreadContext.getCurrentUserName());
		return view;
	}
	
	/**
	 * 到担保人其他详情页面
	 */
	@RequestMapping(value = "/toGuarOthDetailPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toGuarOthDetailPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("project/prjCstAssMbInfo/prjGuarAssMbInfoOthDetail");
		String id = request.getParameter("id");
		PrjCstAssMbInfoVO prjCstAssMbInfoVO = null;
		if(StringUtils.isNotBlank(id)){
			prjCstAssMbInfoVO = this.prjCstAssMbInfoService.selectById(Long.valueOf(id));
		}
		view.addObject("prjCstAssMbInfo", prjCstAssMbInfoVO);
		BaseLogUtils.info(logger, "toModifyPage", "跳转到修改页面.", CurrentThreadContext.getCurrentUserName());
		return view;
	}
	
}

